package cn.wolfcode.web.controller;



import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IBusinessService businessService;


    @RequirePermission(name = "预约页面",expression = "appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //回显高查预约门店
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        //回显高查订单状态
        AppointmentStatusEnum[] enums = AppointmentStatusEnum.values();
        model.addAttribute("enums",enums);
        return "appointment/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "预约删除",expression = "appointment:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            appointmentService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "预约新增/编辑",expression = "appointment:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Appointment appointment){
        if (appointment.getId() != null) {
            appointmentService.update(appointment);
        }else {
            //这个操作时员工页面发来的保存请求,直接设置为履行中
            appointment.setStatus(AppointmentStatusEnum.PERFORM.getValue());
            appointmentService.save(appointment);
        }
        return new JsonResult();
    }

    //只是客户再首先保存的预约订单(不能被拦截)
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Appointment appointment){
        //这个操作是首页用户发来的保存请求,直接设置为待确定
        appointment.setStatus(AppointmentStatusEnum.PEND.getValue());
        appointmentService.save(appointment);
        return new JsonResult();
    }

    //点击确认/取消预约,单独修改状态
    //接收id的同时也接收状态,直接写死再页面,表示修改后的状态
    @RequestMapping("/changeStatus")
    @RequirePermission(name = "预约点状态修改",expression = "appointment:changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Long id,Integer status){
        appointmentService.changeStatus(id,status);
        return new JsonResult();
    }
}
